package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.CustomerCommRecordDTO;
import com.prd.mapper.CustomerCommRecordMapper;
import com.prd.model.CustomerCommRecordDO;
import com.prd.service.CustomerCommRecordService;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户-来往沟通记录 服务实现类
 * </p>
 *
 * @author o1
 * @since 2023-11-26
 */
@Service("CustomerCommRecordService")
public class CustomerCommRecordServiceImpl extends ServiceImpl<CustomerCommRecordMapper, CustomerCommRecordDO> implements CustomerCommRecordService {


    @Autowired
    private CustomerCommRecordMapper recordMapper;


    @Override
    public JsonData uAdd(CustomerCommRecordDTO customerCommRecordDTO) {
        CustomerCommRecordDO recordDO = new CustomerCommRecordDO();
        BeanUtils.copyProperties(customerCommRecordDTO, recordDO);
        recordMapper.insert(recordDO);
        return JsonData.buildSuccess(recordDO);
    }

    @Override
    public JsonData uUpdateAndAdd(CustomerCommRecordDTO customerCommRecordDTO) {
        CustomerCommRecordDO recordDO = recordMapper.selectById(customerCommRecordDTO.getId());
        if (recordDO != null) {
            CopyNonNullBeanUtils.copyProperties(customerCommRecordDTO, recordDO);
            recordMapper.updateById(recordDO);
        }else {
            recordDO = new CustomerCommRecordDO();
            BeanUtils.copyProperties(customerCommRecordDTO, recordDO);
            recordMapper.insert(recordDO);
        }
        return JsonData.buildSuccess(recordDO);
    }

    @Override
    public JsonData update(CustomerCommRecordDO recordDO) {
        CustomerCommRecordDO recordDB = recordMapper.selectById(recordDO.getId());
        if (recordDB != null) {
            CopyNonNullBeanUtils.copyProperties(recordDO, recordDB);
            recordMapper.updateById(recordDB);
            return JsonData.buildSuccess(recordDB);
        }
        else
            return JsonData.buildError("没有找到对应记录");
    }

    @Override
    public JsonData addSimple(CustomerCommRecordDO recordDO) {
        CustomerCommRecordDO recordDB = new CustomerCommRecordDO();
        BeanUtils.copyProperties(recordDO, recordDB);
        recordMapper.insert(recordDB);
        return JsonData.buildSuccess(recordDB);
    }

    @Override
    public List<CustomerCommRecordDTO> uSearchForList(String no) {
        LambdaQueryWrapper<CustomerCommRecordDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerCommRecordDO::getNo, no);
        return recordMapper.selectList(wrapper).stream().map(this::doToDto).collect(Collectors.toList());
    }


    private CustomerCommRecordDTO doToDto(CustomerCommRecordDO recordDO) {
        CustomerCommRecordDTO recordDTO = new CustomerCommRecordDTO();
        BeanUtils.copyProperties(recordDO, recordDTO);
        return recordDTO;
    }
}
